<template>
    <div class="wrapper">

        <!-- header部分 -->
        <header>
            <a href="javascript:history.back();" class="back-button">
                <i class="fa fa-arrow-left"></i> 返回
            </a>
            <p>商家入驻登记</p>
        </header>

        <!-- 表单部分 -->
        <ul class="form-box">
            <li>
                <div class="title">
                    商家名称：
                </div>
                <div class="content">
                    <input type="text" v-model="business.businessName" placeholder="请输入商家名称">
                </div>
            </li>
            <li>
                <div class="title">
                    商家地址：
                </div>
                <div class="content">
                    <input type="text" v-model="business.businessAddress" placeholder="请输入商家地址">
                </div>
            </li>
            <li>
                <div class="title">
                    商家介绍：
                </div>
                <div class="content">
                    <input type="text" v-model="business.businessExplain" placeholder="请输入商家介绍">
                </div>
            </li>
            <li>
                <div class="title">
                    商家备注：
                </div>
                <div class="content">
                    <input type="text" v-model="business.remarks" placeholder="请输入商家备注">
                </div>
            </li>
            <li>
                <div class="title">
                    商家图片：
                </div>
                <div class="Uploader">
                    <input type="file" accept="image/*" @change="onFileChange" style="display: none;" ref="fileInput" />
                    <button @click="uploadImage">上传图片</button>
                    <span v-if="selectedFileName">{{ selectedFileName }}</span>
                </div>
            </li>
            <li>
                <div class=" title">
                    起送价格：
                </div>
                <div class="content">
                    <input type="text" v-model="business.starPrice" placeholder="请输入起送价格">
                </div>
            </li>
            <li>
                <div class="title">
                    配送费用：
                </div>
                <div class="content">
                    <input type="text" v-model="business.deliveryPrice" placeholder="请输入配送费用">
                </div>
            </li>
            <li>
                <div class="title">
                    食物类型：
                </div>
                <div class="content">
                    <label for="options"></label>
                    <select v-model="selectedOption" id="options">
                        <option v-for="option in options" :key="option" :value="option">
                            {{ option }}
                        </option>
                    </select>
                </div>
            </li>
        </ul>

        <div class="button-login">
            <button @click="register">成为商家</button>
        </div>
        <div style="height: 0.1vw;"></div>
        <CustomAlert ref="customAlert" />
        <!-- 底部菜单部分 -->
        <Footer></Footer>
    </div>
</template>

<script>
import Footer from '../components/Footer.vue';
import CustomAlert from '../components/CustomAlert.vue';
import ImageUploader from '../components/ImageUploader.vue';

export default {
    name: 'BeBusinessOwner',
    data() {
        return {
            user: {
                userId: '',
            },
            business: {
                businessName: '',
                businessAddress: '',
                businessExplain: '',
                starPrice: 0,
                deliveryPrice: 0,
                businessImg: '',
                orderTypeId: '',
                userId: '',
                businessImg: ''
            },
            selectedFileName: '',
            selectFile: '',
            selectedOption: '',
            options: ['美食', '早餐', '跑腿代购', '汉堡披萨', '甜品饮料', '速食简餐', '地方小吃', '米粉面馆', '包子粥铺', '炸鸡炸串']
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
    },
    methods: {
        uploadImage() {
            this.$refs.fileInput.click(); // 模拟点击文件输入
        },
        //下面的没有压缩，也许能用，也许不行，没测试过
        onFileChange(event) {
            const file = event.target.files[0]; // 获取上传的文件
            let size = file.size;
            if (size / (1024 * 1024) > 1) {
                this.$refs.customAlert.showAlert('文件大小不能超过1M！')
                return;
            }
            if (file) {
                this.selectedFileName = file.name;
                const reader = new FileReader();
                // 当文件读取完成时，将其转换为URL
                reader.onload = (e) => {
                    let result = e.target.result;
                    result = result.replace(/\//g, '-'); // 将所有的 / 替换为 -
                    result = result.replace(/\+/g, '_'); // 将所有的 + 替换为 _
                    result = result.replace(/\;/g, '~');// 将所有的 ; 替换为 ~
                    this.business.businessImg = result; // 更新头像URL
                    console.log('img:', this.business.businessImg);
                    //this.$setLocalStorage('avatarUrl', this.avatarUrl);
                };
                // 读取文件为Data URL
                reader.readAsDataURL(file);
            }
        },
        checkBusinessOwnerId() {
            this.$axios.get(`Businesses/userId/${this.business.userId}`)
                .then(response => {
                    if (response.data == 1) {
                        this.business.userId = '';
                        this.$refs.customAlert.showAlert('此商家已存在！')
                    }
                }).catch(error => {
                    console.error(error);
                });
        },
        register() {
            if (this.business.businessName == '') {
                this.$refs.customAlert.showAlert('商家名称不能为空！');
                return;
            }
            if (this.business.businessAddress == '') {
                this.$refs.customAlert.showAlert('商家地址不能为空！');
                return;
            }
            if (this.business.businessExplain == '') {
                this.$refs.customAlert.showAlert('商家介绍不能为空！');
                return;
            }
            if (this.business.remarks == '') {
                this.$refs.customAlert.showAlert('商家备注不能为空！');
                return;
            }
            if (this.selectedOption == '') {
                this.$refs.customAlert.showAlert('选项不能为空！');
                return;
            }
            switch (this.selectedOption) {
                case '美食':
                    this.business.orderTypeId = 1;
                    break;
                case '早餐':
                    this.business.orderTypeId = 2;
                    break;
                case '跑腿代购':
                    this.business.orderTypeId = 3;
                    break;
                case '汉堡披萨':
                    this.business.orderTypeId = 4;
                    break;
                case '甜品饮料':
                    this.business.orderTypeId = 5;
                    break;
                case '速食简餐':
                    this.business.orderTypeId = 6;
                    break;
                case '地方小吃':
                    this.business.orderTypeId = 7;
                    break;
                case '米粉面馆':
                    this.business.orderTypeId = 8;
                    break;
                case '包子粥铺':
                    this.business.orderTypeId = 9;
                    break;
                case '炸鸡炸串':
                    this.business.orderTypeId = 10;
                    break;
                default:
                    this.business.orderTypeId = 1;
                    break;
            }

            console.log(this.business.businessImg);
            //商家注册请求：待完善
            this.$axios.post(`Businesses/name/${this.business.businessName}/address/${this.business.businessAddress}/explain/${this.business.businessExplain}/img/${this.business.businessImg}/orderTypeId/${this.business.orderTypeId}/starPrice/${this.business.starPrice}/deliveryPrice/${this.business.deliveryPrice}/remarks/${this.business.remarks}/userId/${this.user.userId}`)
                .then(response => {
                    if (response.data > 0) {
                        //this.$refs.customAlert.showAlert('商家入驻成功！');
                        this.$router.push({ path: '/businessManagement', query: { businessId: response.data } });
                    } else {
                        this.$refs.customAlert.showAlert('商家入驻失败！');
                    }
                }).catch(error => {
                    console.error(error);
                });
        }
    },
    components: {
        Footer,
        CustomAlert,
        ImageUploader
    }
}
</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
    width: 100%;
    height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
    width: 100%;
    height: 12vw;
    background-color: #0097FF;
    color: #fff;
    font-size: 4.8vw;

    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;

    display: flex;
    justify-content: center;
    align-items: center;
}

.back-button {
    position: absolute;
    top: 3.5vw;
    /* 使用相对单位 */
    left: 2.5vw;
    /* 使用相对单位 */
    font-size: 4vw;
    /* 使用相对单位 */
    text-decoration: none;
    color: white;
    display: flex;
    align-items: center;
}

.back-button i {
    margin-right: 1vw;
    /* 使用相对单位 */
}

/* 针对大屏幕 (如 iPad) 的媒体查询 */
@media (min-width: 768px) {
    .wrapper header {
        height: 8vh;
        /* 使用相对高度单位 */
        font-size: 3vh;
    }

    .back-button {
        position: absolute;
        top: 3.5vw;
        /* 使用相对单位 */
        left: 2.5vw;
        /* 使用相对单位 */
        font-size: 4vw;
        /* 使用相对单位 */
        text-decoration: none;
        color: white;
        display: flex;
        align-items: center;
    }

    .back-button i {
        margin-right: 1vw;
        /* 使用相对单位 */
    }

    /* 针对大屏幕 (如 iPad) 的媒体查询 */
    @media (min-width: 768px) {
        .wrapper header {
            height: 8vh;
            /* 使用相对高度单位 */
            font-size: 3vh;
        }

        .back-button {
            top: 2.5vh;
            /* 使用相对高度单位 */
            left: 2vh;
            font-size: 2.5vh;
            /* 相对大小单位 */
        }

        .back-button i {
            margin-right: 0.5vh;
        }
    }

    /****************** 表单部分 ******************/
    .wrapper .form-box {
        width: 100%;
        margin-top: 12vw;
    }

    .back-button i {
        margin-right: 0.5vh;
    }
}

/****************** 表单部分 ******************/
.wrapper .form-box {
    width: 100%;
    margin-top: 12vw;
}

.wrapper .form-box li {
    box-sizing: border-box;
    padding: 4vw 3vw 0 3vw;
    display: flex;
    align-items: center;
}

.wrapper .form-box li .title {
    flex: 0 0 18vw;
    font-size: 3vw;
    font-weight: 700;
    color: #666;
}

.wrapper .form-box li .content {
    flex: 1;
}

.wrapper .form-box li .content input {
    border: none;
    outline: none;
    width: 100%;
    height: 4vw;
    font-size: 3vw;
}

.wrapper .button-login {
    width: 100%;
    box-sizing: border-box;
    padding: 4vw 3vw 0 3vw;
}

.wrapper .button-login button {
    width: 100%;
    height: 10vw;
    font-size: 3.8vw;
    font-weight: 700;
    color: #fff;
    background-color: #38CA73;
    border-radius: 4px;

    border: none;
    outline: none;
}

.wrapper .button-register {
    width: 100%;
    box-sizing: border-box;
    padding: 4vw 3vw 0 3vw;
}

.wrapper .button-register button {
    width: 100%;
    height: 10vw;
    font-size: 3.8vw;
    font-weight: 700;
    color: #666;
    background-color: #EEE;
    border-radius: 4px;

    border: none;
    outline: none;
    border: solid 1px #DDD;
}

label {

    font-size: 3vw;
    color: #4f4e4e;
}

select {
    padding: 5px;
    margin-bottom: 0px;
    font-size: 3vw;
}

.uploader {
    display: flex;
    align-items: center;
    /* 垂直居中对齐 */
}

.upload-button {
    margin-right: 10px;
    /* 按钮与文本之间的间距 */
    padding: 8px 12px;
    /* 按钮内边距 */
    cursor: pointer;
    /* 鼠标悬停时显示手型 */
    background-color: #42b983;
    /* 按钮背景色 */
    color: white;
    /* 按钮文本颜色 */
    border: none;
    /* 去除默认边框 */
    border-radius: 4px;
    /* 按钮圆角 */
    transition: background-color 0.3s;
    /* 背景色过渡效果 */
}

.upload-button:hover {
    background-color: #358a6b;
    /* 悬停时按钮背景色变化 */
}

.file-name {
    font-size: 3vw;
    /* 文件名文本大小 */
    color: #4f4e4e;
    /* 文件名文本颜色 */
}

.Uploader {
    font-size: 3vw;
}
</style>